package com.kunsan.compose.widget

import android.content.Context
import android.util.Log
import android.view.TextureView
import android.view.View
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.kunsan.compose.R
import com.kunsan.compose.ui.theme.LocalUserBackground
import com.kunsan.compose.ui.theme.RemoteUserBackground
import com.kunsan.compose.ui.theme.UserNameBackground


@Composable
fun VideoCell(
    modifier: Modifier = Modifier,
    id: Int?,
    isVideoMuted: Boolean,
    isAudioMuted: Boolean,
    avatarSize: Dp, background: Color,
    createView: ((context: Context) -> View)? = null,
    setupVideo: (renderView: View, id: Int, isFirstSetup: Boolean) -> Unit,
    overlay: @Composable BoxScope.() -> Unit? = { },
    reSetUpVideo: Boolean = false
) {
    Box(modifier) {
        if (id != null) {
            AndroidView(
                factory = { context ->
                    Log.d("VideoCell", "VideoCell: create render view. ${createView?.invoke(context)}")
                    val view = createView?.invoke(context) ?: TextureView(context)
                    view.tag = id
                    setupVideo(view, id, true)
                    view
                },
                update = { view ->
                    Log.d("VideoCell", "VideoCell: create render view reSetUpVideo = $reSetUpVideo")
                    if (reSetUpVideo) {
                        setupVideo(view, id, false)
                    } else if (view.tag != id) {
                        Log.d("VideoCell", "VideoCell: update render view.")
                        view.tag = id
                        setupVideo(view, id, false)
                    }
                }
            )
        }
        MemberProfile(isVideoMuted, background, avatarSize)
        UserAudioState(
            modifier = Modifier
                .align(Alignment.BottomStart),
            isAudioMuted = isAudioMuted
        )
    }
}

@Composable
fun MemberProfile(isVideoMuted: Boolean, background: Color, avatarSize: Dp) {
    if (isVideoMuted) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(background)
        ) {
            Image(
                painter = painterResource(id = R.drawable.ic_launcher_background), modifier = Modifier
                    .size(avatarSize)
                    .clip(CircleShape)
                    .align(Alignment.Center), contentDescription = null
            )
        }
    }
}

@Composable
fun UserAudioState(modifier: Modifier, isAudioMuted: Boolean) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .background(color = UserNameBackground)
    ) {
        Image(
            painter = painterResource(if (isAudioMuted) R.drawable.icon_bottom_audio_off else R.drawable.icon_member_audio_on), modifier = Modifier
                .size(20.dp)
                .padding(4.dp), contentDescription = null
        )

        Text(text = "123", color = Color.White)
    }
}


@Composable
fun TwoVideoView(
    modifier: Modifier = Modifier,
    localUid: Int,
    remoteUid: Int,
    isVideoMuted: Boolean,
    isAudioMuted: Boolean,
    localPrimary: Boolean = true,
    secondClickable: Boolean = false,
    onSecondClick: () -> Unit = {},
    localCreate: ((context: Context) -> View)? = null,
    remoteCreate: ((context: Context) -> View)? = null,
    localRender: (View, Int, Boolean) -> Unit,
    remoteRender: (View, Int, Boolean) -> Unit,
    reSetUpVideo: Boolean = false
) {
    val primary: @Composable (Modifier, Dp) -> Unit = { m, dp ->
        VideoCell(
            modifier = m,
            id = if (localPrimary) localUid else remoteUid,
            createView = if (localPrimary) localCreate else remoteCreate,
            setupVideo = if (localPrimary) localRender else remoteRender,
            isVideoMuted = isVideoMuted,
            isAudioMuted = isAudioMuted,
            reSetUpVideo = reSetUpVideo,
            background = LocalUserBackground,
            avatarSize = dp
        )
    }
    val second: @Composable (Modifier, Dp) -> Unit = { m, dp ->
        VideoCell(
            modifier = if (secondClickable) m.clickable {
                onSecondClick()
            } else m,
            id = if (!localPrimary) localUid else remoteUid,
            createView = if (localPrimary) remoteCreate else localCreate,
            setupVideo = if (!localPrimary) localRender else remoteRender,
            isAudioMuted = isAudioMuted,
            isVideoMuted = isVideoMuted,
            background = RemoteUserBackground,
            avatarSize = dp
        )
    }
    BoxWithConstraints(modifier = modifier) {
        primary(Modifier.fillMaxSize(), maxWidth / 3)
        if (remoteUid != 0) {
            second(
                Modifier
                    .fillMaxWidth(0.4f)
                    .align(Alignment.TopEnd)
                    .aspectRatio(0.57f)
                    .offset(y = 20.dp)
                    .padding(14.dp), maxWidth / 8
            )
        }
    }

}
